<?php

class LinkAction extends PublicAction
{
    protected $linkModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->linkModel = D('Link');
    }

    //列表页
    public function index()
    {
        $map = array();
        $map['lang'] = LANG_ID;
        //取得满足条件的记录总数
        $count = $this->linkModel->where($map)->count('id');

        if ($count > 0) {
            $listRows = I('listRows', C('PAGE_LISTROWS'), 'intval');
            import("@.ORG.Page");
            //创建分页对象
            $p = new Page($count, $listRows);

            //分页查询数据
            $voList = $this->linkModel->where($map)->limit($p->firstRow . ',' . $p->listRows)->select();

            //分页显示
            $page = $p->show();

            //模板赋值显示
            $this->assign('list', $voList);
            $this->assign('page', $page);
        }

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }


    public function add()
    {
        if (IS_POST) {
            $_POST['lang'] = LANG_ID;
            if (empty($_POST['name'])) {
                $this->error('网站名称不能为空！');
            }
            if (empty($_POST['siteurl'])) {
                $this->error('网站地址不能为空！');
            }

            $_POST['create_time'] = time();
            $_POST['update_time'] = $_POST['create_time'];

            if (false === $this->linkModel->create()) {
                $this->error($this->linkModel->getError());
            }

            $_POST['id'] = $id = $this->linkModel->add();

            if ($id !== false) {
                $this->success('添加成功！', cookie('__forward__'));
            } else {
                $this->error('提交失败！: ' . $this->linkModel->getDbError());
            }
        } else {
            $this->display();
        }
    }


    public function edit()
    {
        if (IS_POST) {

            if (empty($_POST))
                $this->error('缺少必要的参数!');

            if (empty($_POST['name'])) {
                $this->error('网站名称不能为空！');
            }
            if (empty($_POST['siteurl'])) {
                $this->error('网站地址不能为空！');
            }

            $_POST['lang'] = LANG_ID;
            if (false === $this->linkModel->create()) {
                $this->error($this->linkModel->getError());
            }

            // 更新数据
            $list = $this->linkModel->save();

            if (false !== $list) {
                $this->success('修改成功!', cookie('__forward__'));
            } else {
                //错误提示
                $this->error('修改失败!: ' . $this->linkModel->getDbError());
            }
        } else {
            $id = $_REQUEST['id'];

            $vo = $this->linkModel->getById($id);

            $vo['content'] = htmlspecialchars($vo['content']);
            $this->assign($_REQUEST);
            $this->assign('vo', $vo);
            $this->display();
        }
    }


    function delete()
    {
        $id = I('get.id');
        if (!$id) {
            $this->error('参数不正确！');
        }

        if (false !== $this->linkModel->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败: ' . $this->linkModel->getDbError());
        }
    }

    public function listorder()
    {
        $ids = $_POST['listorders'];

        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $this->linkModel->where('id=' . $key)->save($data);
        }

        $this->success('提交成功!');
    }
}